//
//  NSString+Regex.m
//  MoYou
//
//  Created by she1323 on 16/10/10.
//  Copyright © 2016年 she1323. All rights reserved.
//

#import "NSString+Regex.h"

@implementation NSString (Regex)


-(BOOL)isValidPhoneNumber{
    NSString *regex = @"1[3|5|7|8|][0-9]{9}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}

///*  判断用户输入的密码是否符合规范，符合规范的密码要求：
//1. 长度大于8位
//2. 密码中必须同时包含数字和字母
//*/
- (BOOL)judgePassWordLegal{
    BOOL result = false;
    if ([self length] >= 8 && [self length] <= 20){
        // 判断长度大于8位后再接着判断是否同时包含数字和字符
        NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        result = [pred evaluateWithObject:self];
    }
    return result;
}

+ (NSString *)isOrNoPasswordStyle:(NSString *)passWordName
{
    
    NSString * message;
    
    if (passWordName.length<8) {
        
        message=@" 密码不能少于8位，请您重新输入";
        
    }
    
    else if (passWordName.length>20)
        
    {
        
        message=@"密码最大长度为20位，请您重新输入";
        
    }
    
    else if ([self JudgeTheillegalCharacter:passWordName])
        
    {
        
        message=@"密码不能包含特殊字符，请您重新输入";
        
    }
    
    else if (![self judgePassWordLegal:passWordName])
        
    {
        
        message=@"密码必须同时包含字母和数字";
        
    }
    
    return message;
    
}

+(BOOL)JudgeTheillegalCharacter:(NSString *)content{
    
    //提示标签不能输入特殊字符
    
    NSString *str =@"^[A-Za-z0-9\\u4e00-\u9fa5]+$";
    
    NSPredicate* emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    
    if (![emailTest evaluateWithObject:content]) {
        
        return YES;
    }
    
    return NO;
    
}

+(BOOL)judgePassWordLegal:(NSString *)pass{
    
    BOOL result ;
    
    // 判断长度大于8位后再接着判断是否同时包含数字和大小写字母
    
    NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    result = [pred evaluateWithObject:pass];
    
    return result;
    
}

@end
